﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class F130 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string v_str_monhocID = Request.QueryString["monhocId"];
        int v_int_monhocID = 0;
        int.TryParse(v_str_monhocID, out v_int_monhocID);

        //if ((v_int_donviID != 0 && v_int_nganhID == 0) || (v_int_donviID == 0 && v_int_nganhID != 0)) Response.Redirect("F530.aspx");
        //if (v_int_donviID == 0 && v_int_nganhID == 0) Response.Redirect("F500.aspx");

        if (!IsPostBack)
        {
            string v_str_gdID = Request.QueryString["Id"];
            int v_int_gdID = 0;
            int.TryParse(v_str_gdID, out v_int_gdID);
            if (v_int_gdID != 0)
            {
                this.m_cmd_tao_moi.Enabled = false;
                this.m_cmd_cap_nhat.Enabled = true;
            }
            else
            {
                this.m_cmd_tao_moi.Enabled = true;
                this.m_cmd_cap_nhat.Enabled = false;
            }
            BindData();
        }
    }

   


    private void BindData()
    {
        try
        {
            m_lbl_thong_bao.Text = "Không có thông báo gì.";

            string v_str_monhocID = Request.QueryString["monhocId"];
            int v_int_monhocID = 0;
            int.TryParse(v_str_monhocID, out v_int_monhocID);

            string v_str_sanphamID = Request.QueryString["spId"];
            int v_int_sanphamID = 0;
            int.TryParse(v_str_sanphamID, out v_int_sanphamID);

            string v_str_ID = Request.QueryString["Id"];
            int v_int_ID = 0;
            int.TryParse(v_str_ID, out v_int_ID);

            if (v_int_ID != 0)
            {
                DM_SAN_PHAM_VERSION entity = Services.GetDM_SAN_PHAM_VERSION(v_int_ID);
                this.txtMaPhienBan.Text = entity.MA_VERSION;
                this.txtTenPhienBan.Text = entity.TEN_VERSION;
                this.txtNgayPhienBan.Text = entity.NGAY_VERSION.ToString("yyyy-MM-dd");
                this.txtGhiChu.Text = entity.GHI_CHU;
                if (entity.DA_XUAT_BAN_YN == "Y")
                {
                    this.ckTrangThai.Checked = true;
                }
                else this.ckTrangThai.Checked = false;
            }

            string v_str_info = "####";
            if ((v_int_monhocID != 0))
            {
                DM_MON_HOC v_obj_DM_MON_HOC = Services.GetDM_MON_HOCById(v_int_monhocID);
                DM_SAN_PHAM v_obj_DM_SAN_PHAM = Services.GetDM_SAN_PHAM(v_int_sanphamID);
                v_str_info = (!String.IsNullOrEmpty(v_obj_DM_MON_HOC.TEN_TIENG_VIET) ? v_obj_DM_MON_HOC.TEN_TIENG_VIET : "######") + " -> " + (!String.IsNullOrEmpty(v_obj_DM_SAN_PHAM.TEN_SAN_PHAM) ? v_obj_DM_SAN_PHAM.TEN_SAN_PHAM : "######");
                this.lblMonHocInfo.Text = v_str_info + " -> Danh sách phiên bản. ";
            }

            this.lblF221.Text = v_str_info + " ->  F130 - Thêm mới phiên bản";

            List<GetPhienBanByMonHocSanPhamForGridResult> lst = Services.GetPhienBanByMonHocSanPhamForGrid(v_int_monhocID, v_int_sanphamID);

            if (lst != null)
            {
                gvDsMonHoc.DataSource = lst;
                gvDsMonHoc.DataBind();
                // Set event for Delete button
                foreach (GridViewRow row in gvDsMonHoc.Rows)
                {
                    ImageButton imageButton = (ImageButton)row.FindControl("imgBtDelete");
                    imageButton.Attributes.Add("OnClick", "javascript:return confirm('Bạn có chắc chắn muốn xóa phiên bản này?')");
                }
            }
            else
            {

            }

        }
        catch (Exception)
        {

        }

    }

    protected void gvDsMonHoc_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string v_str_monhocID = Request.QueryString["monhocId"];
        int v_int_monhocID = 0;
        int.TryParse(v_str_monhocID, out v_int_monhocID);

        string v_str_sanphamID = Request.QueryString["spId"];
        int v_int_sanphamID = 0;
        int.TryParse(v_str_sanphamID, out v_int_sanphamID);

        int id = Convert.ToInt32(e.CommandArgument);

        switch (e.CommandName.ToUpper())
        {
            case "EDIT":
                Response.Redirect(String.Format("F130.aspx?monhocId={0}&spId={1}&Id={2}", v_int_monhocID, v_int_sanphamID, id));
                break;
            case "DEL":
                bool isok = Services.DeleteDM_SAN_PHAM_VERSION(id);
                if (isok)
                {
                    Response.Redirect(Request.Url.ToString());
                }
                else
                {
                    this.m_lbl_mess.Text = "Lỗi, xóa không thành công!";
                    m_lbl_mess.Visible = true;
                }
                break;
        }
    }

    protected void m_cmd_tao_moi_Click(object sender, EventArgs e)
    {
        try
        {
            string v_str_monhocID = Request.QueryString["monhocId"];
            int v_int_monhocID = 0;
            int.TryParse(v_str_monhocID, out v_int_monhocID);
            if (v_int_monhocID == 0)
            {
                this.m_lbl_mess.Text = "Bạn phải Kích chọn Môn học trong Cây môn học - sản phẩm.";
                this.m_lbl_mess.Visible = true;
                return;
            }
            string v_str_sanphamID = Request.QueryString["spId"];
            int v_int_sanphamID = 0;
            int.TryParse(v_str_sanphamID, out v_int_sanphamID);


            DM_SAN_PHAM_VERSION entity = new DM_SAN_PHAM_VERSION();
            entity.ID_MON_HOC_SAN_PHAN = Services.GetMON_HOC_SAN_PHAM(v_int_monhocID, v_int_sanphamID).ID;
            entity.MA_VERSION = this.txtMaPhienBan.Text;
            entity.TEN_VERSION = this.txtTenPhienBan.Text;
            
            entity.NGAY_VERSION = DateTime.ParseExact(this.txtNgayPhienBan.Text, "yyyy-MM-dd", System.Globalization.CultureInfo.CurrentCulture); 

            if (this.ckTrangThai.Checked)
            {
                entity.DA_XUAT_BAN_YN = "Y";
            }
            else
            {
                entity.DA_XUAT_BAN_YN = "N";
            }
            entity.GHI_CHU = this.txtGhiChu.Text;

            bool isOk = Services.InsertDM_SAN_PHAM_VERSION(entity);
            if (isOk)
            {
                Response.Redirect(Request.RawUrl.ToString());
            }
            else
            {
                this.m_lbl_mess.Text = "Đã có lỗi xảy ra khi thêm mới sản phẩm môn học.";
                this.m_lbl_mess.Visible = true;
            }
        }
        catch (Exception ex)
        {
            Logger.Instance.Write(ex);
        }

    }
    private void ClearForm()
    {

    }

    protected void m_cmd_cap_nhat_Click(object sender, EventArgs e)
    {
        string v_str_ID = Request.QueryString["Id"];
        int v_int_ID = 0;
        int.TryParse(v_str_ID, out v_int_ID);
        if (v_int_ID != 0)
        {
            try
            {
                string v_str_monhocID = Request.QueryString["monhocId"];
                int v_int_monhocID = 0;
                int.TryParse(v_str_monhocID, out v_int_monhocID);

                string v_str_sanphamID = Request.QueryString["spId"];
                int v_int_sanphamID = 0;
                int.TryParse(v_str_sanphamID, out v_int_sanphamID);


                DM_SAN_PHAM_VERSION entity = new DM_SAN_PHAM_VERSION();
                entity.ID = v_int_ID;
                entity.ID_MON_HOC_SAN_PHAN = Services.GetMON_HOC_SAN_PHAM(v_int_monhocID, v_int_sanphamID).ID;
                entity.MA_VERSION = this.txtMaPhienBan.Text;
                entity.TEN_VERSION = this.txtTenPhienBan.Text;
                entity.NGAY_VERSION = DateTime.ParseExact(this.txtNgayPhienBan.Text, "yyyy-MM-dd", System.Globalization.CultureInfo.CurrentCulture); ;

                if (this.ckTrangThai.Checked)
                {
                    entity.DA_XUAT_BAN_YN = "Y";
                }
                else
                {
                    entity.DA_XUAT_BAN_YN = "N";
                }
                entity.GHI_CHU = this.txtGhiChu.Text;

                bool isOk = Services.UpdateDM_SAN_PHAM_VERSION(entity);
                if (isOk)
                {
                    Response.Redirect(Request.RawUrl.ToString());
                }
                else
                {
                    this.m_lbl_mess.Text = "Đã có lỗi xảy ra khi thêm mới phiên bản.";
                    this.m_lbl_mess.Visible = true;
                }
            }
            catch (Exception ex)
            {
                Logger.Instance.Write(ex);
            }
        }
        else
        {
            this.m_lbl_mess.Text = "Bạn hãy chọn sản phẩm cần chỉnh sửa.";
        }
    }
    protected void gvDsMonHoc_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvDsMonHoc.PageIndex = e.NewPageIndex;
        BindData();
    }
}